package com.yuanfengerp.basicInfoManage.customerInfo.service.impl;

import com.yuanfengerp.basicInfoManage.customerInfo.dao.ICustomerinfoDao;
import com.yuanfengerp.basicInfoManage.customerInfo.model.Customerinfo;
import com.yuanfengerp.basicInfoManage.customerInfo.service.ICustomerinfoservice;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.sql.Timestamp;

@Service
public class CustomerinfoserviceImpl implements ICustomerinfoservice {
    @Autowired
    ICustomerinfoDao icust;
    @Override
    public Result<List<Customerinfo>> selectCustomerAll(Map<String ,Object> params) {
        params.put("pagenum",((int) params.get("pagenum")-1) * (int) params.get("num"));
        List<Customerinfo> customerinfos = icust.selectCustomerAll(params);
        if (!(customerinfos.isEmpty())){
            return Result.success(customerinfos, icust.Count());
        }
        return Result.failure(400,"没有数据");
    }

    @Override
    public Result<String> addCustomer(Customerinfo customerinfo) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0);
        customerinfo.setCustomer_id(idWorker.nextId());
        customerinfo.setCustomer_time(new Timestamp(System.currentTimeMillis()));
        customerinfo.setCustomer_amendtime(new Timestamp(System.currentTimeMillis()));
        Integer integer = icust.addCustomer(customerinfo);
        if (integer > 0){
            return Result.success("添加成功");
        }
        return Result.failure(400,"添加失败");
    }

    @Override
    public Result<String> updateCustomer(Customerinfo customerinfo) {
        customerinfo.setCustomer_amendtime(new Timestamp(System.currentTimeMillis()));
        Integer integer = icust.updateCustomer(customerinfo);
        if (integer > 0){
            return Result.success("修改成功");
        }
        return Result.failure(400,"修改失败");
    }

    @Override
    public Result<Customerinfo> selectCustomer(String id) {
        Customerinfo customerinfo = icust.selectCustomer(id);
        if (customerinfo != null){
            return Result.success(customerinfo);
        }
        return Result.failure(400,"没找到");
    }
}
